using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace SS.core
{
	internal class groupProducts
	{
		private Func objFunc = new Func();
		private string strConnection = "";
		public groupProducts()
		{
			Program.strConnectionString = this.objFunc.strConnectionString;
		}
		public void loadToDataGridView(DataGridView dgv, BindingNavigator bdn)
		{
			dgv.AutoGenerateColumns = false;
			SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
			sqlConnection.Open();
			string selectCommandText = "SELECT GpName,GpNote,GpID FROM [TabGproducts] ORDER BY [GpID]";
			SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(selectCommandText, sqlConnection);
			DataSet dataSet = new DataSet();
			sqlDataAdapter.Fill(dataSet, "TabGproducts");
			BindingSource bindingSource = new BindingSource();
			bindingSource.DataSource = dataSet.Tables["TabGproducts"];
			bdn.BindingSource = bindingSource;
			dgv.DataSource = bindingSource;
			sqlConnection.Close();
			sqlConnection.Dispose();
		}
		public void loadToListView(ListView lv, string searchFor)
		{
			lv.Items.Clear();
			SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
			sqlConnection.Open();
			string cmdText = "SELECT GpName,GpNote,GpID FROM [TabGproducts] WHERE [GpName] LIKE N'%" + searchFor + "%' ORDER BY [GpID]";
			SqlCommand sqlCommand = new SqlCommand(cmdText, sqlConnection);
			SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
			int num = 0;
			while (sqlDataReader.Read())
			{
				lv.Items.Add((num + 1).ToString());
				lv.Items[num].UseItemStyleForSubItems = false;
				lv.Items[num].SubItems.Add(sqlDataReader["GpName"].ToString());
				lv.Items[num].SubItems.Add(sqlDataReader["GpNote"].ToString());
				lv.Items[num].SubItems.Add(sqlDataReader["GpID"].ToString());
				num++;
			}
			sqlDataReader.Close();
			sqlDataReader.Dispose();
			sqlConnection.Close();
			sqlConnection.Dispose();
		}
		public void getInfo(string gID, TextBox TxtGpName, TextBox TxtGpNote)
		{
			SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
			sqlConnection.Open();
			string cmdText = "SELECT GpName,GpNote FROM [TabGproducts] WHERE [GpID] = " + gID.ToString();
			SqlCommand sqlCommand = new SqlCommand(cmdText, sqlConnection);
			SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
			while (sqlDataReader.Read())
			{
				TxtGpName.Text = sqlDataReader["GpName"].ToString();
				TxtGpNote.Text = sqlDataReader["GpNote"].ToString();
			}
			sqlDataReader.Close();
			sqlDataReader.Dispose();
			sqlConnection.Close();
			sqlConnection.Dispose();
		}
		public int getCount()
		{
			int result = 0;
			try
			{
				SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
				sqlConnection.Open();
				string cmdText = "SELECT COUNT(*) AS CountGproducts FROM [TabGproducts]";
				SqlCommand sqlCommand = new SqlCommand(cmdText, sqlConnection);
				SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
				while (sqlDataReader.Read())
				{
					result = int.Parse(sqlDataReader["CountGproducts"].ToString());
				}
				sqlDataReader.Close();
				sqlDataReader.Dispose();
				sqlConnection.Close();
				sqlConnection.Dispose();
			}
			catch
			{
			}
			return result;
		}
		public int del(string id)
		{
			int result;
			try
			{
				SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
				sqlConnection.Open();
				SqlCommand sqlCommand = new SqlCommand("DelGroupProducts", sqlConnection);
				sqlCommand.CommandType = CommandType.StoredProcedure;
				sqlCommand.Parameters.Add("ID", SqlDbType.VarChar);
				sqlCommand.Parameters["ID"].Value = id;
				sqlCommand.ExecuteNonQuery();
				sqlConnection.Close();
				sqlConnection.Dispose();
				result = 0;
			}
			catch
			{
				result = 1;
			}
			return result;
		}
	}
}
